package com.iemes.controller.forming;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.OrderperFormMap;
import com.iemes.entity.ProgressFormMap;
import com.iemes.entity.TableFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.craft.TableMapper;
import com.iemes.mapper.forming.ChangeShiftsMapper;
import com.iemes.mapper.forming.MoInstructionMapper;
import com.iemes.mapper.forming.ProgressMapper;
import com.iemes.mapper.order.OrderpreMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;
@Controller
@RequestMapping("progress")
public class ProgressController extends BaseController{
	@Inject
	private ProgressMapper progressMapper;
	@Inject 
	private ChangeShiftsMapper shiftsMapper;
	@Inject
	private MoInstructionMapper instructionMapper;
	@Inject
	private TableMapper tableMapper;
	@Inject
	private OrderpreMapper orderpreMapper;
	
	@RequestMapping(value = "statistics", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		//如果不是主管
		if (userFormMap.get("site").equals("0")) {
			//进入页面操作
			return Common.BACKGROUND_PATH + "/forming/progress/statistics_regulatorAdd";		
		}
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/forming/progress/statistics_list";	
	}
	
	@ResponseBody
	@RequestMapping("statistics_findByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		ProgressFormMap progressFormMap =getFormMap(ProgressFormMap.class);
		progressFormMap = toFormMap(progressFormMap, pageNow, pageSize,
				progressFormMap.getStr("orderby"));
		List<ProgressFormMap> findByPage = progressMapper.findByPage(progressFormMap);
		pageView.setRecords(findByPage);
		return pageView;
	}
	
	@RequestMapping("statistics_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		//1.先查出点检内容下的点检项目id
		ProgressFormMap findbyFrist = progressMapper.findbyFrist("id", id, ProgressFormMap.class);
		model.addAttribute("progress", findbyFrist);	
		return Common.BACKGROUND_PATH + "/forming/progress/statistics_edit";
	}

	@ResponseBody
	@RequestMapping(value = "statistics_editEntity")
	@SystemLog(module = "成型管理", methods = "交接班检查-修改交接班检查")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		ProgressFormMap progressFormMap = getFormMap(ProgressFormMap.class);
		progressFormMap.put("Date", DateUtils.getStringDateTime());
		progressMapper.editEntity(progressFormMap);		
		return "success";
	}
	@ResponseBody
	@RequestMapping(value = "statistics_deleteEntity")
	@SystemLog(module = "成型管理", methods = "交接班检查-删除交接班检查")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		ProgressFormMap progressFormMap = new ProgressFormMap();
		for (int i = 0; i < str.length; i++) {
			progressFormMap.put("Status", 2);
			progressFormMap.put("id", str[i]);
			progressFormMap.put("Date", DateUtils.getStringDateTime());
			progressMapper.editEntity(progressFormMap);
		}
		return "success";
	}
	//调机员上模时的操作
	@ResponseBody
	@RequestMapping("regulator_search")
	public Map<String,Object> search(Model model) throws Exception {
		//当模具开始上模时，则要查出机台数据和预排表中订单编号
		TableFormMap tableFormMap = new TableFormMap();
		//机台号
		tableFormMap.put("machine_no", getPara("Machine_Number"));
		//模具号
		tableFormMap.put("mold_Number", getPara("Mold_Number"));
		//当日日期
		tableFormMap.put("Date",DateUtils.getStringDate());
		List<Map<String,Object>> tableMolding = tableMapper.getTableMolding(tableFormMap);
		String	dateString= tableMolding.get(0).get("日期").toString().replace("/", "-")+" "+ tableMolding.get(0).get("时间");				
		long usedTime = DateUtils.stringToDateTime(dateString).getTime();
		//查询订单号
		OrderperFormMap orderperFormMap = new OrderperFormMap();
		orderperFormMap.put("machine_no", getPara("Machine_Number"));
		orderperFormMap.put("now_mold_encode", getPara("Mold_Number"));
		orderperFormMap.put("state", 5);
		List<OrderperFormMap> findByNames = orderpreMapper.findByNames(orderperFormMap);
		tableMolding.get(0).put("DateTime", usedTime);
		tableMolding.get(0).put("Order_id", findByNames.get(0).get("order_id"));
		return tableMolding.get(0);
	}
	
		//调机员上模时的操作
		@ResponseBody
		@RequestMapping("statistics_addEntity")
		public String addEntity(Model model) throws Exception {
			ProgressFormMap progressFormMap = getFormMap(ProgressFormMap.class);
			//需要计算调机检查时间
			String recordingTime =getPara("recordingTime") ;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String format = sdf.format(new Date(Long.valueOf(recordingTime)));
			TableFormMap tableFormMap = new TableFormMap();
			tableFormMap.put("machine_no", progressFormMap.get("Machine_Number"));
			tableFormMap.put("mold_Number", progressFormMap.get("Mold_Number"));
			tableFormMap.put("startDate", format);
			tableFormMap.put("termDate", DateUtils.getStringDateTime());
			List<Map<String, Object>> tableDate = tableMapper.getTableDate(tableFormMap);
			if (tableDate.size()!=0) {
				long time = DateUtils.stringToDateTime(tableDate.get(0).get("Date").toString().replace("/", "-")).getTime();
				progressFormMap.put("Check_Machine", time-Long.valueOf(recordingTime)/(1000*60));	
			}
			progressFormMap.put("Status", 1);
			progressFormMap.put("Date", DateUtils.getStringDateTime());
			progressMapper.addEntity(progressFormMap);
			return "success";
		}
}
